package com.tzstcl.gen.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLDecoder;
import java.sql.*;
import java.util.*;

/**
 * @ClassName FormManualBuilder
 * @Description TODO
 * @Author yuelh
 * @Date 2019/3/4 14:36
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
public class FormManualBuilder {
    private static Connection connection;
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://192.168.5.210:3306/xt5-test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8","root","123456");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
     }
    public static List<String> getAllTableNames(){
        String getTablesSql = "select table_name from information_schema.tables where table_schema='xt5-test'";
        try {
            PreparedStatement pstmt = connection.prepareStatement(getTablesSql);
            ResultSet resultSet = pstmt.executeQuery();
            while(resultSet.next()){
                String tableName = resultSet.getString("table_name");
                System.out.println(tableName);
            }


        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

        return null;
    }

    public  static  String getTableComment(String tableName){
        String getTableCommentSql = "select table_comment from information_schema.tables where table_schema='xt5-test' and table_name='"+tableName+"'";
        String tableComment="数据库表中未添加注释，表单标题获取失败";
        try {
            PreparedStatement pstmt = connection.prepareStatement(getTableCommentSql);
            ResultSet resultSet = pstmt.executeQuery();
            while(resultSet.next()){
                tableComment = resultSet.getString("table_comment");
            }
            return tableComment;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

        return null;
    }


    public static List<FormBase> getTableColumns(String tableName){
        List<FormBase> formBaseList = new ArrayList<>();
        Set<String> baseColumns = new HashSet<>();
        baseColumns.add("id");
        baseColumns.add("uid");
        baseColumns.add("create_by");
        baseColumns.add("create_time");
        baseColumns.add("update_by");
        baseColumns.add("update_time");
        baseColumns.add("del_flag");
        baseColumns.add("org_id");
        String getColumn = "select COLUMN_NAME,COLUMN_COMMENT from information_schema.COLUMNS where table_name = '"+tableName+"' and table_schema = 'xt5-test'";
        PreparedStatement pstmt = null;
        try {
            pstmt = connection.prepareStatement(getColumn);
            ResultSet resultSet = pstmt.executeQuery();
            while(resultSet.next()){
                String columnName = resultSet.getString("COLUMN_NAME");
                String columnComment = resultSet.getString("COLUMN_COMMENT");
                if(!baseColumns.contains(columnName)){
                    FormBase formBase = new FormBase();
                    formBase.setInputName(CamelUtils.underline2Camel(columnName));
                    formBase.setLabelName(columnComment);
                    formBase.setComment(columnComment);
                    formBaseList.add(formBase);
                }
            }
            return formBaseList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void buildPage(String tableNme){
        List<FormBase> formBaseList = getTableColumns(tableNme);
        String formName = getTableComment(tableNme);
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
        String path = FormManualBuilder.class.getResource("/").getPath() + "templates/gen/curd";
        try {
            //模版路径
            cfg.setDirectoryForTemplateLoading(new File(URLDecoder.decode(path, "UTF-8")));
            //通用审批页面
            Template template = cfg.getTemplate("/approval-form.ftl");
            Map<String,Object> root = new HashMap<>();
            root.put("formBaseList",formBaseList);
            root.put("formName",formName);
            File file = new File("D:/src/form/"+CamelUtils.underline2Camel(tableNme)+"Form.html");
            if(!file.exists()){
                file.createNewFile();
            }
            template.process(root,new FileWriter(file));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }


    }

    public void buildAllFormPage(){

    }

    public static void main(String[] args) {
        buildPage("per_basic_info");
    }



}
